<?php

namespace App\Http\Controllers\Finance;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Models\Finance\Regulation;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

/**
 * 薪资方案计算规则逻辑控制器
 *
 * Date: 2018/6/8
 * @author George
 * @package App\Http\Controllers\Finance\
 */
class RegulationController extends Controller
{
	/**
	 * 获取指定薪资方案的计算规则
	 *
	 * Date: 2018/6/8
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function index(Request $request)
	{
		$this->validate($request, [
			'pattern_id' => 'required',
		]);
		$user = Auth::user();
		$query = Regulation::query();
		$regulations = $query->select([
			'regulations.id as regulation_id',
			'regulations.company_id',
			'regulations.pattern_id',
			'regulations.field_id',
			'regulations.template_id',
			'regulations.type',
			'regulations.value',
			'regulations.expression',
			'regulations.literals',
			'fields.name as field_name',
			'templates.name as template_name'
		])->where([
			['regulations.company_id', '=', $user->company_id],
			['regulations.pattern_id', '=', $request->get('pattern_id')]
		])
			->leftJoin('fields', 'regulations.field_id', '=', 'fields.id')
			->leftJoin('templates', 'regulations.template_id', '=', 'templates.id')
			->get();

		return success($regulations);
	}

	/**
	 * 获取规则详情信息
	 *
	 * Date: 2018/6/8
	 * @author George
	 * @param $regulation
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function show($regulation)
	{
		$regulation = Regulation::query()->where('regulations.id', $regulation)
			->leftJoin('fields', 'regulations.field_id', '=', 'fields.id')
			->leftJoin('templates', 'regulations.template_id', '=', 'templates.id')
			->first();
		return success($regulation);
	}

	/**
	 * 创建薪资字段计算规则
	 *
	 * Date: 2018/6/8
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function store(Request $request)
	{
		$attributes = $this->validate($request, [
			'pattern_id' => 'required',
			'field_id' => 'required|unique:regulations',
			'reference_id' => 'required_if:type,logic',
			'template_id' => 'required_if:type,logic',
			'type' => [
				'required',
				Rule::in(array_keys(config('finance.regulation.type')))
			],
			'value' => 'required_if:type,value|numeric',
			'expression' => 'required_if:type,sample',
			'literals' => 'required_if:type,sample'
		], [
			'pattern_id.required' => '未选择薪资方案',
			'reference_id.required_if' => '请选择条件参照字段',
			'field_id.required' => '逻辑表达式请选择参考字段',
			'field_id.unique' => '该字段已经设置了规则',
			'template_id.required_if' => '逻辑表达式请选择条件',
			'type.required' => '请选择规则类型',
			'type.in' => '规则类型有误',
			'value.required_if' => '请输入固定值',
			'expression.required_if' => '请输入表达式',
			'literals.required_if' => '请传递表达式字面量',
		]);

		$user = Auth::user();
		$attributes['company_id'] = $user->company_id;
		$regulation = Regulation::create($attributes);
		return stored($regulation);
	}

	/**
	 * 更新薪资计算规则属性
	 *
	 * Date: 2018/6/8
	 * @author George
	 * @param Request $request
	 * @param Regulation $regulation
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function update(Request $request, Regulation $regulation)
	{
		$attributes = $this->validate($request, [
			'field_id' => [
				'required',
				Rule::unique('regulations')->ignore($regulation->id)
			],
			'reference_id' => 'required_if:type,logic',
			'template_id' => 'required_if:type,logic',
			'type' => [
				'required',
				Rule::in(array_keys(config('finance.regulation.type')))
			],
			'value' => 'required_if:type,value|numeric',
			'expression' => 'required_if:type,sample',
			'literals' => 'required_if:type,sample'
		], [
			'reference_id.required_if' => '请选择条件参照字段',
			'field_id.required' => '逻辑表达式请选择参考字段',
			'field_id.unique' => '该字段已经设置了规则',
			'template_id.required_if' => '逻辑表达式请选择条件',
			'type.required' => '请选择规则类型',
			'type.in' => '规则类型有误',
			'value.required_if' => '请输入固定值',
			'expression.required_if' => '请输入表达式',
			'literals.required_if' => '请传递表达式字面量',
		]);

		$regulation->update($attributes);
		return updated($regulation);
	}

	/**
	 * 删除薪资计算规则
	 *
	 * Date: 2018/6/8
	 * @author George
	 * @param Regulation $regulation
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Exception
	 */
	public function destroy(Regulation $regulation)
	{
		$regulation->delete();
		return deleted();
	}
}
